﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TMS
{
    public partial class WorkOrderControl : UserControl
    {
        Product prod = new Product();
        Users users = new Users();
        Users ActiveUser = Program.GetActiveUser;
        Partner partner = new Partner();
        string ProjectID;
        public DataGridViewRow parameters;
        DataTable table = new DataTable();
        bool WithParameters = false;
        bool edit = false;
        Project project = new Project();
        string NoteNumber;

        public WorkOrderControl()
        {
            Program.GetMainForm.Text = "TMS - Работни налози";

            InitializeComponent();
            table = prod.GetAllWorkOrders().Tables[0];
            table.DefaultView.RowFilter = string.Format("Responsible ='" + ActiveUser.GetName()+" "+ ActiveUser.GetSurname()+ "'");
            dgvDispatchList.DataSource = table;

            dgvDispatchList.Columns[0].Visible = false;
            dgvDispatchList.Columns[1].HeaderText = "Датум";
            dgvDispatchList.Columns[2].HeaderText = "Одговорен";
            dgvDispatchList.Columns[3].HeaderText = "Налог бр.";
            dgvDispatchList.Columns[3].DisplayIndex = 1;
            dgvDispatchList.Columns[4].Visible = false;
            dgvDispatchList.Columns[5].HeaderText = "Статус";
            dgvDispatchList.Columns[6].HeaderText = "Локација";
            dgvDispatchList.Columns[7].HeaderText = "Рок на завршување";
            dgvDispatchList.Columns[8].HeaderText = "Раб. задача";
            dgvDispatchList.Columns[9].HeaderText = "Извршил";
            dgvDispatchList.Columns[10].HeaderText = "Проект";
            dgvDispatchList.Columns[10].DisplayIndex = 4;

            //btnNew.Visible = false;

            FillResponsibels();
           

        }

        public WorkOrderControl(DataGridViewRow parametersin)
        {
            Program.GetMainForm.Text = "TMS - Работни налози";

            InitializeComponent();
            WithParameters = true;
            parameters = parametersin;
            ProjectID = parameters.Cells[0].Value.ToString();
            dgvDispatchList.DataSource = prod.GetAllWorkOrders(ProjectID).Tables[0];



            dgvDispatchList.Columns[0].Visible = false;
            dgvDispatchList.Columns[1].HeaderText = "Датум";
            dgvDispatchList.Columns[2].HeaderText = "Одговорен";
            dgvDispatchList.Columns[3].HeaderText = "Налог бр.";
            dgvDispatchList.Columns[3].DisplayIndex = 1;
            dgvDispatchList.Columns[4].Visible=false;
            dgvDispatchList.Columns[5].HeaderText = "Статус";
            dgvDispatchList.Columns[6].HeaderText = "Локација";
            dgvDispatchList.Columns[7].HeaderText = "Рок на завршување";
            dgvDispatchList.Columns[8].HeaderText = "Раб. задача";
            dgvDispatchList.Columns[9].HeaderText = "Извршил";
            dgvDispatchList.Columns[10].HeaderText = "Проект";
            dgvDispatchList.Columns[10].DisplayIndex = 4;
            ProjectID = parameters.Cells[0].Value.ToString();
            project.ID =Convert.ToInt32(ProjectID);
            txtProject.Text = project.GetName();
            FillResponsibels();


            
        }

        

        public void FillResponsibels()
        {
            DataTable table = users.GetAllUserNameAndSurname();
            foreach (DataRow row in table.Rows)
            {
                cmbResponsible.Items.Add(row.ItemArray[0].ToString());
                cmbResponsible.Items.Remove("Администратор ");
                cmbWorkers.Items.Add(row.ItemArray[0].ToString());
                cmbWorkers.Items.Remove("Администратор ");
                
            }
            cmbResponsible.SelectedItem = ActiveUser.GetName() + " " + ActiveUser.GetSurname();
            cmbWorkers.SelectedItem = ActiveUser.GetName() + " " + ActiveUser.GetSurname();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            
        }

        private void dgvDispatchList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_WORKORDER_CONTROL, dgvDispatchList.CurrentRow);
            Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_WORKORDER_CONTROL, dgvDispatchList.CurrentRow);

            this.Dispose();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            
        }

       
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvDispatchList.SelectedRows.Count == 0)
                MessageBox.Show("Нематате селектирано налог");
            else
            {

                if (dgvDispatchList.SelectedRows[0].Cells[5].Value.ToString() == "Затворен")
                    MessageBox.Show("Налогот е веќе затворен");
                else
                    if (ActiveUser.GetName() + " " + ActiveUser.GetSurname() != dgvDispatchList.SelectedRows[0].Cells[2].Value.ToString())
                        MessageBox.Show("Не сте одговорен за избраниот налог");

                    else
                    {
                        prod.DeleteWorkOrder(Convert.ToInt32(dgvDispatchList.SelectedRows[0].Cells[3].Value));
                        this.Dispose();
                        if (WithParameters)
                            Program.GetMainForm.ShowControl(ControlsEnum.WORKORDER_CONTROL, parameters);
                        else
                            Program.GetMainForm.ShowControl(ControlsEnum.WORKORDER_CONTROL);
                    }
           }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (dgvDispatchList.SelectedRows.Count > 0)
            {
                cmbResponsible.SelectedItem = dgvDispatchList.SelectedRows[0].Cells[2].Value.ToString();
                ProjectID = dgvDispatchList.SelectedRows[0].Cells[4].Value.ToString();
                project.ID = Convert.ToInt32(ProjectID);
                txtProject.Text = project.GetName();
                txtLocation.Text = dgvDispatchList.SelectedRows[0].Cells[6].Value.ToString();
                txtAsignment.Text = dgvDispatchList.SelectedRows[0].Cells[8].Value.ToString();
                cmbWorkers.SelectedItem = dgvDispatchList.SelectedRows[0].Cells[9].Value.ToString();

            }
            grNew.Visible = true;
            edit = false;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (!edit)
                prod.AddWordOrder(prod.GetLastWorkOrder() + 1, cmbResponsible.SelectedItem.ToString(), dtpDate.Value, ProjectID, "Отворен", txtLocation.Text, dtFinishDate.Value, txtAsignment.Text, cmbWorkers.SelectedItem.ToString());
            else
            {
                prod.UpdateWorkOrder(dtpDate.Value, cmbResponsible.SelectedItem.ToString(), txtLocation.Text, dtFinishDate.Value, txtAsignment.Text, cmbWorkers.SelectedItem.ToString(), NoteNumber);
            }
            this.Dispose();
            if (WithParameters)
                Program.GetMainForm.ShowControl(ControlsEnum.WORKORDER_CONTROL, parameters);
            else
                Program.GetMainForm.ShowControl(ControlsEnum.WORKORDER_CONTROL);

        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (dgvDispatchList.SelectedRows.Count == 0)
                MessageBox.Show("Нематате селектирано налог");
            else
            {

                if (dgvDispatchList.SelectedRows[0].Cells[5].Value.ToString() == "Затворен")
                    MessageBox.Show("Налогот е веќе затворен");
                else
                    if (ActiveUser.GetName() + " " + ActiveUser.GetSurname() != dgvDispatchList.SelectedRows[0].Cells[2].Value.ToString())
                        MessageBox.Show("Не сте одговорен за избраниот налог");

                    else
                    {
                        cmbResponsible.SelectedItem = dgvDispatchList.SelectedRows[0].Cells[2].Value.ToString();
                        ProjectID = dgvDispatchList.SelectedRows[0].Cells[4].Value.ToString();
                        project.ID = Convert.ToInt32(ProjectID);
                        txtProject.Text = project.GetName();
                        txtLocation.Text = dgvDispatchList.SelectedRows[0].Cells[6].Value.ToString();
                        txtAsignment.Text = dgvDispatchList.SelectedRows[0].Cells[8].Value.ToString();
                        dtpDate.Value = Convert.ToDateTime(dgvDispatchList.SelectedRows[0].Cells[1].Value);
                        dtpDate.Value = Convert.ToDateTime(dgvDispatchList.SelectedRows[0].Cells[7].Value);
                        cmbWorkers.SelectedItem = dgvDispatchList.SelectedRows[0].Cells[9].Value.ToString();
                        NoteNumber = dgvDispatchList.SelectedRows[0].Cells[3].Value.ToString();
                        edit = true;
                        grNew.Visible = true;
                    }
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (dgvDispatchList.SelectedRows.Count == 0)
                MessageBox.Show("Нематате селектирано налог");
            else
            {

                if (dgvDispatchList.SelectedRows[0].Cells[5].Value.ToString() == "Затворен")
                    MessageBox.Show("Налогот е веќе затворен");
                else
                    if (ActiveUser.GetName() + " " + ActiveUser.GetSurname() != dgvDispatchList.SelectedRows[0].Cells[2].Value.ToString())
                        MessageBox.Show("Не сте одговорен за избраниот налог");

                    else
                    {
                        prod.DeleteWorkOrder(Convert.ToInt32(dgvDispatchList.SelectedRows[0].Cells[3].Value));
                        this.Dispose();
                        if (WithParameters)
                            Program.GetMainForm.ShowControl(ControlsEnum.WORKORDER_CONTROL, parameters);
                        else
                            Program.GetMainForm.ShowControl(ControlsEnum.WORKORDER_CONTROL);
                    }
            }
        }

        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            if (dgvDispatchList.SelectedRows.Count == 0)
                MessageBox.Show("Нематате селектирано налог");
            else
            {

                if (dgvDispatchList.SelectedRows[0].Cells[5].Value.ToString() == "Затворен")
                    MessageBox.Show("Налогот е веќе затворен");
                else
                    if (ActiveUser.GetName() + " " + ActiveUser.GetSurname() != dgvDispatchList.SelectedRows[0].Cells[2].Value.ToString())
                        MessageBox.Show("Не сте одговорен за избраниот налог");

                    else
                    {
                        prod.CloseWorkOrder(Convert.ToInt32(dgvDispatchList.SelectedRows[0].Cells[3].Value));
                        this.Hide();
                        if (WithParameters)
                            Program.GetMainForm.ShowControl(ControlsEnum.WORKORDER_CONTROL, parameters);
                        else
                            Program.GetMainForm.ShowControl(ControlsEnum.WORKORDER_CONTROL);
                    }
            }
        }

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back()) 
            this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }

       

        
    }
}
